package cn.agrael.struts.plugin.ejb3;

import static cn.agrael.struts.plugin.ejb3.StrutsEJBPluginConstant.EAR_FILE_BASE_NAME;
import static cn.agrael.struts.plugin.ejb3.StrutsEJBPluginConstant.ENC_PATH;
import static cn.agrael.struts.plugin.ejb3.StrutsEJBPluginConstant.JNDI_PATH_DELIMITER;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.NamingException;

import cn.agrael.util.StringUtils;

/**
 * Jboss 应用服务器的实现。
 * 
 * @author <a href="mailto:agraellee@gmail.com">Agrael·Lee</a>
 * 
 */
public class JbossApplicationServer extends AbstractApplicationServer {

	protected void buildEJBBeanNameJndiPath(Class<?> clazz, StringBuilder jndiNameBuilder, Class<?> ejbType, EJB ejb) {
		jndiNameBuilder.append(EAR_FILE_BASE_NAME);
		jndiNameBuilder.append(JNDI_PATH_DELIMITER);
		jndiNameBuilder.append(ejb.beanName());
		jndiNameBuilder.append(JNDI_PATH_DELIMITER);
		if (Object.class != ejb.beanInterface()) {
			// 使用了 beanInterface 属性则从 beanInterface 中提取本地/远程接口
			Class<?> interfaceClass = ejb.beanInterface();
			jndiNameBuilder.append(StringUtils.checkEmptyString(StrutsEJBUtils.findLocalOrRemote(interfaceClass), ""));
		} else {
			// 找寻类注解上的本地/远程注解
			jndiNameBuilder.append(StringUtils.checkEmptyString(StrutsEJBUtils.findLocalOrRemote(ejbType), ""));
		}
	}

	protected void buildEJBMappedNameJndiPath(Class<?> clazz, StringBuilder jndiNameBuilder, Class<?> ejbType, EJB ejb) {
		jndiNameBuilder.append(ejb.mappedName());
	}

	protected void buildEJBNameJndiPath(Class<?> clazz, StringBuilder jndiNameBuilder, Class<?> ejbType, EJB ejb) {
		jndiNameBuilder.append(ENC_PATH);
		jndiNameBuilder.append(ejb.name());
	}

	protected void buildResourceMappedNameJndiPath(Class<?> clazz, StringBuilder jndiNameBuilder, Class<?> resourceType, Resource resource) {
		jndiNameBuilder.append(resource.mappedName());
	}

	protected void buildResourceNameJndiPath(Class<?> clazz, StringBuilder jndiNameBuilder, Class<?> resourceType, Resource resource) {
		jndiNameBuilder.append(ENC_PATH);
		jndiNameBuilder.append(resource.name());
	}

	protected Object globalLookup(StringBuilder jndiNameBuilder, InjectAnnotationWrapper injectAnnotationWrapper, Class<?> clazz,
			Context context) throws NamingException, Exception {
		if (clazz.isInterface()) {
			jndiNameBuilder.append(EAR_FILE_BASE_NAME);
			jndiNameBuilder.append(JNDI_PATH_DELIMITER);
			jndiNameBuilder.append(clazz.getSimpleName());
			jndiNameBuilder.append(JNDI_PATH_DELIMITER);
			jndiNameBuilder.append(StrutsEJBUtils.findLocalOrRemote(clazz));
			throw new NamingException();
		} else {
			Class<?>[] interfaces = clazz.getInterfaces();
			String localOrRemote = null;
			for (Class<?> interfaceClass : interfaces) {
				// 找到的第一个本地/远程接口作为需要的参数，优先查找本地接口
				if ((localOrRemote = StrutsEJBUtils.findLocalOrRemote(interfaceClass)) != null) {
					break;
				}
			}
			if (localOrRemote == null) {
				throw new Exception();
			}
			jndiNameBuilder.append(EAR_FILE_BASE_NAME);
			jndiNameBuilder.append(JNDI_PATH_DELIMITER);
			jndiNameBuilder.append(clazz.getSimpleName());
			jndiNameBuilder.append(JNDI_PATH_DELIMITER);
			jndiNameBuilder.append(localOrRemote);
			return context.lookup(jndiNameBuilder.toString());
		}
	}
}